LEADTOOLS Barcode (Leadtools.Barcode assembly)

ReadBarcodes(RasterImage,LeadRect,Int32,BarcodeSymbology[],IBarcodeReadOptions[]) Method

Show in webframe
Example 







A Leadtools.RasterImage object that contains the image data. Must not be null.
A Leadtools.LeadRect that specifies the region of interest area in the image where the barcodes search and detection is performed. You can specify empty rectangle to indicate that the search must be performed on the whole image.
An System.Int32 that specifies the maximum number of barcodes to return. Must be a value greater than or equal to 0. The value of 0 means all barcodes.
An array of BarcodeSymbology enumeration members that specifies the barcode symbologies (types) to search for.
An array of IBarcodeReadOptions that specifies the options to use. This can be null, where the current default options is used. Otherwise, the method will use the options that corresponds to the symbologies being read, if the array does not contain specific options for a symbologies being read, then the default version will be used.

For information about this method please see ReadBarcodes(RasterImage,LogicalRectangle,Int32,BarcodeSymbology[],BarcodeReadOptions[]).

Syntax
'Declaration
 
Public Overloads Function ReadBarcodes( _
   ByVal image As RasterImage, _
   ByVal searchBounds As LeadRect, _
   ByVal maximumBarcodes As Integer, _
   ByVal symbologies() As BarcodeSymbology, _
   ByVal options() As IBarcodeReadOptions _
) As BarcodeData()
'Usage
 
Dim instance As BarcodeReader
Dim image As RasterImage
Dim searchBounds As LeadRect
Dim maximumBarcodes As Integer
Dim symbologies() As BarcodeSymbology
Dim options() As IBarcodeReadOptions
Dim value() As BarcodeData
 
value = instance.ReadBarcodes(image, searchBounds, maximumBarcodes, symbologies, options)

            

            
 function Leadtools.Barcode.BarcodeReader.ReadBarcodes(RasterImage,LeadRect,Int32,BarcodeSymbology[],IBarcodeReadOptions[])( 
   image ,
   searchBounds ,
   maximumBarcodes ,
   symbologies ,
   options 
)

Parameters

image
A Leadtools.RasterImage object that contains the image data. Must not be null.
searchBounds
A Leadtools.LeadRect that specifies the region of interest area in the image where the barcodes search and detection is performed. You can specify empty rectangle to indicate that the search must be performed on the whole image.
maximumBarcodes
An System.Int32 that specifies the maximum number of barcodes to return. Must be a value greater than or equal to 0. The value of 0 means all barcodes.
symbologies
An array of BarcodeSymbology enumeration members that specifies the barcode symbologies (types) to search for.
options
An array of IBarcodeReadOptions that specifies the options to use. This can be null, where the current default options is used. Otherwise, the method will use the options that corresponds to the symbologies being read, if the array does not contain specific options for a symbologies being read, then the default version will be used.

Return Value

An array of BarcodeData objects that contains the symbology, data, location and any rotation angle for each barcode found. If no barcodes can be found, then this method will return an empty array (Length equals to 0).
Remarks

Note: In LEADTOOLS for .NET, the equivalent to Leadtools.LeadRect is Leadtools.Forms.LogicalRectangle and the equivalent to IBarcodeReadOptions is BarcodeReadOptions.

Example
Copy Code  
using Leadtools;
using Leadtools.Codecs;
using Leadtools.Barcode;
using Leadtools.ImageProcessing;

// This class holds the data we will pass to each thread
public class MyThreadData
{
   // This worker thread name
   public string Name;
   // Barcode reader instance
   public BarcodeReader BarcodeReaderInstance;

   // Array of options to use
   public IBarcodeReadOptions[] ReadOptions;

   // Image file containing the barcodes
   public string ImageFileName;

   // This will hold the barcodes found
   public BarcodeData[] Barcodes;

   // In case of errors
   public Exception Error;

   // RasterImage for ImageFileName
   public RasterImage Image;
}

      
public async Task BarcodeReader_ReadBarcodeExample6()
{
   string originalImageFileName = @"Assets\Barcode1.tif";
   string rotatedImageFileName = @"Barcode1_Rotated90.tif";

   string[] imageFileNames = 
   {
      originalImageFileName,
      rotatedImageFileName
   };

   // Create a 90 degrees rotated version of Barcode1.tif
   StorageFile loadFile = null;
   using (RasterCodecs codecs = new RasterCodecs())
   {
      loadFile = await Tools.AppInstallFolder.GetFileAsync(originalImageFileName);
      using (RasterImage image = await codecs.LoadAsync(LeadStreamFactory.Create(loadFile)))
      {
         RotateCommand rotate = new RotateCommand(90 * 100, RotateCommandFlags.Resize, RasterColorHelper.FromKnownColor(RasterKnownColor.White));
         rotate.Run(image);

         StorageFile saveFile = await Tools.AppLocalFolder.CreateFileAsync(rotatedImageFileName);
         await codecs.SaveAsync(image, LeadStreamFactory.Create(saveFile), RasterImageFormat.CcittGroup4, 1);
      }
   }

   // Create a Barcode engine
   BarcodeEngine engine = new BarcodeEngine();

   // Get the Barcode reader instance
   BarcodeReader reader = engine.Reader;

   // Get a version of all read options that support rotated barcodes
   IBarcodeReadOptions[] verticalBarcodeReadOptions = GetVerticalReadBarcodeOptions(reader);

   // Tasks to wait for
   Task[] finishedTasks = new Task[2];

   // Create the thread data objects
   MyThreadData[] threadsData = new MyThreadData[2];
   for(int i = 0; i < threadsData.Length; i++)
   {
      threadsData[i] = new MyThreadData();
      threadsData[i].BarcodeReaderInstance = reader;
      threadsData[i].ReadOptions = null;
   }

   // Setup the read options for the two threads
   using (RasterCodecs codecs = new RasterCodecs())
   {
      threadsData[0].Name = "Read Default Options Thread";
      threadsData[0].ReadOptions = null;  // Default, or horizontal
      loadFile = await Tools.AppInstallFolder.GetFileAsync(originalImageFileName);
      threadsData[0].Image = await codecs.LoadAsync(LeadStreamFactory.Create(loadFile));

      threadsData[1].Name = "Read Vertical Barcodes Thread";
      threadsData[1].ReadOptions = verticalBarcodeReadOptions;
      loadFile = await Tools.AppLocalFolder.GetFileAsync(rotatedImageFileName);
      threadsData[1].Image = await codecs.LoadAsync(LeadStreamFactory.Create(loadFile));
   }

   List<BarcodeData> totalBarcodes = new List<BarcodeData>();

   // Now loop through all the images and try to read the barcodes with both threads at the same time
   foreach(string imageFileName in imageFileNames)
   {
      Debug.WriteLine("Reading barcodes from {0}", imageFileName);

      for(int i = 0; i < 2; i++)
      {
         // Clear the previous results (if any), set up the file name and run
         threadsData[i].Barcodes = null;
         threadsData[i].Error = null;
         threadsData[i].ImageFileName = imageFileName;
         finishedTasks[i] = Task.Factory.StartNew(ReadBarcodesInThread, threadsData[i]);
      }

      // Wait till all the tasks finishes
      Task.WaitAll(finishedTasks);

      // Add the barcodes found to our list
      for (int i = 0; i < 2; i++)
      {
         if (threadsData[i].Error != null)
         {
            Debug.WriteLine("{0} had error {1}", threadsData[i].Name, threadsData[i].Error);
         }
         else if (threadsData[i].Barcodes != null)
         {
            totalBarcodes.AddRange(threadsData[i].Barcodes);
         }
      }
   }

   // We are done, show the total number of barcodes read
   Debug.WriteLine("Done. Total barcodes read: {0}", totalBarcodes.Count);
}

private void ReadBarcodesInThread(object state)
{
   MyThreadData threadData = state as MyThreadData;

   Debug.WriteLine("{0} is reading the barcodes", threadData.Name);

   try
   {
      // Read the barcodes using our options
      BarcodeSymbology[] symbologies = { BarcodeSymbology.Unknown };
      threadData.Barcodes = threadData.BarcodeReaderInstance.ReadBarcodes(threadData.Image, LeadRectHelper.Empty, 0, symbologies, null);

      Debug.WriteLine("{0} has read {1} barcodes", threadData.Name, threadData.Barcodes.Length);
   }
   catch (Exception ex)
   {
      // Return the error to main
      threadData.Error = ex;
   }
   finally
   {
   }
}

private static IBarcodeReadOptions[] GetVerticalReadBarcodeOptions(BarcodeReader reader)
{
   // By default, the options read horizontal barcodes only, create an array of options capable of reading vertical barcodes

   // Notice, we cloned the default options in reader so we will not change the original options

   OneDBarcodeReadOptions oneDReadOptions = reader.GetDefaultOptions(BarcodeSymbology.UPCA).Clone() as OneDBarcodeReadOptions;
   oneDReadOptions.SearchDirection = BarcodeSearchDirection.Vertical;

   FourStateBarcodeReadOptions fourStateReadOptions = reader.GetDefaultOptions(BarcodeSymbology.USPS4State).Clone() as FourStateBarcodeReadOptions;
   fourStateReadOptions.SearchDirection = BarcodeSearchDirection.Vertical;

   PostNetPlanetBarcodeReadOptions postNetPlanetReadOptions = reader.GetDefaultOptions(BarcodeSymbology.PostNet).Clone() as PostNetPlanetBarcodeReadOptions;
   postNetPlanetReadOptions.SearchDirection = BarcodeSearchDirection.Vertical;

   GS1DatabarStackedBarcodeReadOptions gs1StackedReadOptions = reader.GetDefaultOptions(BarcodeSymbology.GS1DatabarStacked).Clone() as GS1DatabarStackedBarcodeReadOptions;
   gs1StackedReadOptions.SearchDirection = BarcodeSearchDirection.Vertical;

   PatchCodeBarcodeReadOptions patchCodeReadOptions = reader.GetDefaultOptions(BarcodeSymbology.PatchCode).Clone() as PatchCodeBarcodeReadOptions;
   patchCodeReadOptions.SearchDirection = BarcodeSearchDirection.Vertical;

   PDF417BarcodeReadOptions pdf417ReadOptions = reader.GetDefaultOptions(BarcodeSymbology.PDF417).Clone() as PDF417BarcodeReadOptions;
   pdf417ReadOptions.SearchDirection = BarcodeSearchDirection.Vertical;

   MicroPDF417BarcodeReadOptions microPdf417ReadOptions = reader.GetDefaultOptions(BarcodeSymbology.MicroPDF417).Clone() as MicroPDF417BarcodeReadOptions;
   microPdf417ReadOptions.SearchDirection = BarcodeSearchDirection.Vertical;

   // Even though this array will not contain all options, it should be enough to read all barcodes, since the version of ReadBarcodes we will use
   // will use the default options if an overriden is not passed
   IBarcodeReadOptions[] readOptions =
   {
      oneDReadOptions, fourStateReadOptions, postNetPlanetReadOptions, gs1StackedReadOptions, patchCodeReadOptions, pdf417ReadOptions, microPdf417ReadOptions
   };

   return readOptions;
}
Requirements

Target Platforms

See Also

Reference

BarcodeReader Class
BarcodeReader Members
Overload List

 

 


Products | Support | Contact Us | Copyright Notices
© 2006-2014 All Rights Reserved. LEAD Technologies, Inc.

Leadtools.Barcode requires a Barcode Module license and unlock key. For more information, refer to: LEADTOOLS Toolkit Features